package Four;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

/**
 * 客户端
 * @author 长风无言
 */
public class Send {

    public static void main(String[] args) {

        Socket s = null;
        ObjectInputStream in = null;
        ObjectOutputStream out = null;

        try {
            //连接上服务器
            s = new Socket("127.0.0.1", 8888);
            System.out.println("连接成功");
            //创建并初始化数据
            UserMessage userMessage = new UserMessage("check", new User("admin", "123456"));
            //关联上输出流
            out = new ObjectOutputStream(s.getOutputStream());
            //发送数据给服务器
            out.writeObject(userMessage);
            //关联输入流
            in = new ObjectInputStream(s.getInputStream());
            //将服务器发回来的数据强转
            userMessage = (UserMessage)in.readObject();
            //判断数据里面服务器修改后的类型是成功还是失败并给出对应登录提示
            if ("success".equals(userMessage.getCheckFlag())) {
                System.out.println("登陆成功");
            }else{
                System.out.println("登陆失败");
            }
        } catch (IOException |ClassNotFoundException e) {
            e.printStackTrace();
        } finally{
            if (null!=in) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null!=out) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null!=s) {
                try {
                    s.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
